﻿using System.Windows;
using System.Windows.Controls;

namespace CustomModal.Controls
{
    public class Modal : ContentControl
    {
        public bool IsOpen
        {
            get { return (bool)GetValue(IsVisibleProperty); }
            set { SetValue(IsVisibleProperty, value); }
        }

        public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.Register("IsOpen", typeof(bool), typeof(Modal), new PropertyMetadata(OnIsOpenChanged));

        private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            VisualStateManager.GoToState((Modal)d, (bool)e.NewValue ? "On" : "Off", false);
        }

        public override void OnApplyTemplate()
        {
            VisualStateManager.GoToState(this, this.IsOpen ? "On" : "Off", false);
            base.OnApplyTemplate();
        }

        protected override void OnTap(System.Windows.Input.GestureEventArgs e)
        {
            e.Handled = true;
            base.OnTap(e);
        }
    }
}